package med.movyzer.core;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import med.movyzer.io.GT3XData;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;



public class MovyzerMain extends JFrame{
	ChartPanel chartPanel;

	public MovyzerMain() {
		this.getContentPane().setLayout(null);
		this.initWindow();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.pack();
	}	
	protected void initWindow() 
	{
		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);

		JMenu menu = new JMenu("File");
		menuBar.add(menu);
		
		JMenuItem menuItem = new JMenuItem("Open GT3X-File");
		menuItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				openGT3XFile();				
			}
		});
		menu.add(menuItem);

		chartPanel = new ChartPanel(null);
		chartPanel.setPreferredSize(new java.awt.Dimension(800, 270));
		
		setContentPane(chartPanel);
	}
	
	private void openGT3XFile(){
		FileDialog dialog = new FileDialog(new Frame());
		dialog.setTitle("Select Sensor file");
		dialog.setFilenameFilter(null);
		dialog.setVisible(true);
		File file = new File(dialog.getDirectory(), dialog.getFile());
		
		GT3XData gt3xdata=new GT3XData();
		try {
			gt3xdata.loadFile(file.getPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JFreeChart chart = ChartFactory.createTimeSeriesChart(
				"Sample XY Chart",  // Title
				"Height",           // X-Axis label
				"Weight",           // Y-Axis label
				gt3xdata.getMovementDataSet(50),          // Dataset
				true,
				true,
				false                // Show legend
		);
		chart.setTitle("Graph");
		chartPanel.setChart(chart);
	}

	public static void main(String[] args) {
		MovyzerMain m=new MovyzerMain();
	}

	
}